$(document).ready(function() {
	
	
	var currentDraggedItem;
	var tempTable = "";
	tempTable += '<table id="tempDragTable" cellspacing=0 cellpadding=2 border=1><tbody></tbody></table>' ;
	$('body').append(tempTable);
	
	$("#dnd-example").treeTable();

	$( "#dnd-example tbody" ).sortable({
		revert: true,
		refreshPositions: true, // Performance?
		stop: function(event, ui) {
			//console.log(currentDraggedItem.prev());
			var aboveItem = currentDraggedItem.prev();
			//console.log(aboveItem.position().left);
			//var belowItem = currentDraggedItem.next();
			//console.log(belowItem.position().left);
			//console.log(currentDraggedItem);
			//console.log(event.pageX);
			//console.log(currentDraggedItem.position().left);
			//console.log(aboveItem.find("td:first-child").find("span.menu_item").position().left);
			//console.log(aboveItem);
			//var aboveLeftPosition = aboveItem.find("td:first-child").find("span.menu_item").position().left;
			/*
			var currentLeftPosition = event.pageX;
			if((currentLeftPosition - aboveLeftPosition) >= 25){
				//console.log(parseInt(aboveItem.find("td:first-child").css("padding-left"),10));
				var abovePaddingLeft = parseInt(aboveItem.find("td:first-child").css("padding-left"),10);
				currentDraggedItem.find("td:first-child").css("padding-left",abovePaddingLeft+25);	
				currentDraggedItem.addClass("child-of-"+aboveItem.attr("id"));
			}else{
				var abovePaddingLeft = parseInt(aboveItem.find("td:first-child").css("padding-left"),10);
				currentDraggedItem.find("td:first-child").css("padding-left",abovePaddingLeft);				
			}
			*/
			if(typeof aboveItem.attr("id") !== "undefined"){
				if(!currentDraggedItem.hasClass("child-of-"+aboveItem.attr("id"))){
					var abovePaddingLeft = parseInt(aboveItem.find("td:first-child").css("padding-left"),10);
					currentDraggedItem.find("td:first-child").css("padding-left",abovePaddingLeft);	
				}
			}else{
				var belowItem = currentDraggedItem.next();
				var belowPaddingLeft = parseInt(belowItem.find("td:first-child").css("padding-left"),10);
				currentDraggedItem.find("td:first-child").css("padding-left",belowPaddingLeft);
			}
			//console.log($( "#dnd-example tbody tr:regex(class, .*child.*)"));
			//$($(ui.draggable).parents("tr")[0]).appendBranchTo(currentDraggedItem);
			/*
			$(".child-of-"+currentDraggedItem.attr("id")).each(function(){
				//console.log($(this));
				var abovePaddingLeft = parseInt(currentDraggedItem.find("td:first-child").css("padding-left"),10);				
				$(this).insertAfter(currentDraggedItem);
				$(this).find("td:first-child").css("padding-left",abovePaddingLeft+25);		
			});
			*/
			moveChildren(currentDraggedItem);
		}
	});
	
	function moveChildren(currentDraggedItem){
		$(".child-of-"+currentDraggedItem.attr("id")).each(function(){
			//console.log(currentDraggedItem);
			var abovePaddingLeft = parseInt(currentDraggedItem.find("td:first-child").css("padding-left"),10);				
			$(this).insertAfter(currentDraggedItem);
			$(this).find("td:first-child").css("padding-left",abovePaddingLeft+25);	
			
			//Recusive move children
			if($(".child-of-"+$(this).attr("id")).size() > 0){
				moveChildren($(this));
			}
		});
		//moveChildren(this);
	}	
	
    // Drag & Drop Example Code
	
    $("#dnd-example .menu_item").draggable({
      helper: "clone",
      opacity: .75,
      refreshPositions: true, // Performance?
      revert: "invalid",
      revertDuration: 300,
      scroll: true,
      containment: 'window',
	  start: function(event, ui){
		currentDraggedItem = $(this).parent().parent();
	  },
	  drag: function (event, ui){
		var parentId = $(this).parent().parent().attr("id");
		//console.log(".child-of-"+parentId);
		//$(".child-of-"+currentDraggedItem.attr("id")).addClass("ui-sortable-helper");
		//$(".child-of-"+currentDraggedItem.attr("id")).css("position","absolute");
		//$(".child-of-"+parentId).css("left",event.pageX);
		//$(".child-of-"+parentId).css("top",event.pageY);
		$(".child-of-"+currentDraggedItem.attr("id")).each(function (){
			$("#tempDragTable tbody").append(this);
		});
		
		//var distantX;
		//var distantY;
		//var originalX = currentDraggedItem.find("td:first-child").find("span.menu_item").position().left;
		//var originalY = currentDraggedItem.find("td:first-child").find("span.menu_item").position().top;
		/*
		//Distant X caculation
		if(event.pageX > originalX){
			distantX = event.pageX - originalX;
			$(".child-of-"+currentDraggedItem.attr("id")).each(function(){
				var currentLeft = parseInt($(this).css("left"),10);		
				$(this).css("left",currentLeft+distantX);
			});
		}else if(event.pageX < originalX){
			distantX = originalX - event.pageX;
			$(".child-of-"+currentDraggedItem.attr("id")).each(function(){
				var currentLeft = parseInt($(this).css("left"),10);		
				$(this).css("left",currentLeft - distantX);
			});
		}else{
			distantX = originalX;
		}
		//Distant Y caculation
		if(event.pageY > originalY){
			distantY = event.pageY - originalY;
			$(".child-of-"+currentDraggedItem.attr("id")).each(function(){
				var currentTop = parseInt($(this).css("top"),10);		
				$(this).css("top",currentTop + distantY);
			});
		}else if(event.pageY < originalY){
			distantY = originalY - event.pageY;
			$(".child-of-"+currentDraggedItem.attr("id")).each(function(){
				var currentTop = parseInt($(this).css("top"),10);		
				$(this).css("top",currentTop - distantY);
			});
		}else{
			distantY = originalY;
		}
		*/
	  },
      stop: function (event, ui){
    	  //console.log($(this).offsetLeft);
    	 // currentDraggedItem = $(this).parent().parent();
    	  //console.log($(this).parent().parent().attr("id"));
    	// var currentTrID = $(this).parent().parent().attr("id");
    	 //console.log($("#dnd-example tr#"+currentTrID).prev().prev().prev());
    	 //var x = $( "#dnd-example tbody tr#"+ currentTrID);
    	 //console.log(ui.helper.parent().parent().prev());
      }
    });
    
    
    $("#dnd-example tbody tr").droppable({
    	hoverClass:"accept",
        drop: function (event, ui) {
        	console.log(currentDraggedItem);
        	var parentPaddingLeft = parseInt($(this).find("td:first-child").css("padding-left"),10);
			currentDraggedItem.find("td:first-child").css("padding-left",parentPaddingLeft+25);	
			currentDraggedItem.insertAfter(this);
			currentDraggedItem.addClass("child-of-"+$(this).attr("id"));
        },
    });
    
	/*
    $("#dnd-example .menu_item").each(function() {
        $($(this).parents("tr")[0]).droppable({
          accept: ".menu_item",
          drop: function(e, ui) { 
        	  alert("sdffsdsdf");
            //$($(ui.draggable).parents("tr")[0]).appendBranchTo(this);
            $("#dnd-example .ui-draggable-dragging").remove();
          },
         
        });
      });
    $( "#dnd-example tr" ).disableSelection();
    */
    // Make visible that a row is clicked
    $("table#dnd-example tbody tr").mousedown(function() {
      $("tr.selected").removeClass("selected"); // Deselect currently selected rows
      $(this).addClass("selected");
    });
	
    // Make sure row is selected when span is clicked
    $("table#dnd-example tbody tr span").mousedown(function() {
      $($(this).parents("tr")[0]).trigger("mousedown");
    });
  });